<template>
  <x-dialog
    :visible="visible"
    title="更新vip"
    @close="handleClose(false)"
  >
    <x-page-form ref="pageForm" :form="form" :config="vipConfig" :rules="rules" @submit="onSubmit" >
      <template #vipStartTime>
        <el-date-picker v-model="form.vipStartTime" type="date" placeholder="选择日期"></el-date-picker>
      </template>
      <template #vipEndTime>
        <el-date-picker v-model="form.vipEndTime" type="date" placeholder="选择日期"></el-date-picker>
      </template>
    </x-page-form>
  </x-dialog>
</template>

<script>
import * as axiosApi from '../api'
import { vipConfig } from '../config'
export default {
  props: {
    visible: Boolean,
    visibleType: {
      type: String,
      default: ''
    },
    model: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: { vipStartTime:'',
      vipEndTime:'',
      vip:false
      },
      vipConfig,
      rules: {
        vipStartTime: [
          { required: true, message: '请输选择起始时间' }
        ],
        vipEndTime: [
          { required: true, message: '请输选择结束时间' }
        ]
      }
    }
  },
  watch: {
    visible(newValue, oldValue) {
      if (newValue) {
        Object.keys(this.form).forEach(key => {
          console.log(this.model.vipGrade[key]);
          if (this.model.vipGrade[key]) this.form[key] = this.model.vipGrade[key]
        })
      } else {
        this.initForm()
      }
    }
  },
  methods: {
    handleClose(update) {
      this.$emit('close', update)
    },
    initForm() {
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
        if (item === 'vip') this.form[item] = false
      })
    },
    onSubmit({ validate }) {
      validate((valid) => {
        if (valid) {
          axiosApi.updateVip(this.model.userNo, this.form).then(() => {
            this.$message({
              message: '新增成功'
            })
            this.handleClose(true)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray:#889aa4;
.show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
</style>
